/**
 * 公用js
 */
 $.fn.bootstrapTable.utils.sprintf;

var filterType = {
    /**
     * 等于
     */
	SEARCH_EQ : 'search_eq_',
    /**
     * 左模糊
     */
	SEARCH_LLIKE : 'search_llike_',
    /**
     * 右模糊
     */
	SEARCH_RLIKE : 'search_rlike_',
    /**
     * 全模糊
     */
	SEARCH_LIKE : 'search_like_'
};

var fileinputDefaults = {
    language: 'zh', //设置语言
    uploadUrl: ctx + "common/sysFile/upload", //上传的地址
    deleteUrl: ctx + "common/sysFile/remove", //删除的地址
    allowedFileTypes : ['image'],
    //allowedFileExtensions: ['jpg'],//接收的文件后缀
    uploadAsync: true, //默认异步上传
    showUpload: true, //是否显示上传按钮
    showRemove : false, //显示移除按钮
    showPreview : true, //是否显示预览
    showDownload: false, //是否显示下载
    showCaption: false,//是否显示标题
    allowedPreviewTypes : [ 'image' ],
    initialPreviewAsData: true,
    initialPreviewFileType: 'image',
    dropZoneTitle: "请通过拖拽图片文件放到这里", 
    dropZoneClickTitle: "或者点击此区域添加图片",
    browseClass: "btn btn-primary", //按钮样式
    removeClass: 'btn btn-default',
    cancelClass: 'btn btn-default',
    uploadClass: 'btn btn-default',
    dropZoneEnabled: true,//是否显示拖拽区域
    maxFileCount: 15, //表示允许同时上传的最大文件个数
    enctype: 'multipart/form-data',
    validateInitialCount: true,
    minImageWidth: 50,
    minImageHeight: 50,
    previewSettings: {
    	image: {
    		width: "auto", 
    		height: "140px"
		}
    }
};

/**
 * 校验正负正数就返回true
 **/
function isIntNum(val) {
    var regPos = /^\d+$/; // 非负整数
    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if(regPos.test(val)){
        return true;
    } else {
        return false;
    }
}

function getCategory(category, value) {
	if (value && Bee.StringUtils.contains(value, ",")) {
		var values = value.split(",");
	} else if (value) {
		var values = [value];
	} else {
		return {};
	}
	var result = [];
	var dictCategory = loadCategory(category);
	for (var i in values) {
		result.push(dictCategory[values[i]]);
	}
	return result.join(",");
}

function loadCategory(category) {
	var cacheCategory = parent.$('body').data('cacheCategory') || {};
	var dictCategory = cacheCategory[category];
	if ($.isEmptyObject(dictCategory)) {
	    $.ajax({
	        url : ctx + 'module/common/dict/list/' + category,
	        async : false,
	        success : function(data) {
	        	dictCategory = {};
	            //加载数据
	            for (var i = 0; i < data.length; i++) {
	            	dictCategory[data[i].value] = data[i].name;
	            }
	            cacheCategory[category] = dictCategory;
	            parent.$('body').data('cacheCategory', cacheCategory);
	        }
	    });
	}
	return dictCategory;
}
function loadDictCssStyle(type) {
	var cacheDictCssStyle = parent.$('body').data('cacheDictCssStyle') || {};
	var dictCategory = cacheDictCssStyle[type];
	if ($.isEmptyObject(dictCategory)) {
		$.ajax({
			url : ctx + 'module/common/dict/list/' + type,
			async : false,
			success : function(data) {
				dictCategory = {};
				//加载数据
				for (var i = 0; i < data.length; i++) {
					dictCategory[data[i].value] = data[i].cssClass;
				}
				cacheDictCssStyle[type] = dictCategory;
				// console.log('cacheDictCssStyle',cacheDictCssStyle)
				parent.$('body').data('cacheDictCssStyle', cacheDictCssStyle);
			}
		});
	}
	return dictCategory;
}


!function ($) {
	var sprintf = $.fn.bootstrapTable.utils.sprintf;
	$.fn.ajaxChosen = function(options) {
		var $jq = $(this);
		var category = options.category, 
		    editable = options.editable,
		    group = options.group,
		    initValue = $jq.attr("value") || $jq.val(),
		    isMultiple = $jq.attr("multiple");
		if (group) {
			isMultiple = true,
			$jq.attr("multiple", "multiple");
		}
		if (isMultiple && initValue) {
			var array = initValue.split(",");
		}
		if (category) {
			var cacheCategory = parent.$('body').data('cacheCategory') || {};
			var dictCategory = cacheCategory[category];
			if ($.isEmptyObject(dictCategory)) {
				dictCategory = {};
				$.ajax({
			        url : ctx + 'module/common/dict/list/' + category,
			        async : false,
			        success : function(data) {
		                for (var i = 0; i < data.length; i++) {
		                	dictCategory[data[i].value] = data[i].name;
		                }
		                cacheCategory[category] = dictCategory;
		                parent.$('body').data('cacheCategory', cacheCategory);
			        }
			    });
			}
			
			var options = [];
			for (var value in dictCategory) {
	    		if (array && $.inArray(value, array) != -1) {
	    			options.push(sprintf('<option value="%s" %s>%s</option>', 
		    				value, 'selected="selected"', dictCategory[value]));
	    		} else {
	    			options.push(sprintf('<option value="%s">%s</option>', value, dictCategory[value]));
	    		}
	    	}
			
			var html = "";
			if (group && $.isArray(group)) {
    			for (var i in group) {
    				var item = group[i];
    				html += sprintf('<optgroup label="%s">', item.label);
    				$.each(options, function(i, option) {
    					if (Bee.StringUtils.contains(option, item.key)) {
    						html += option;
    					}
    				});
    				html += '</optgroup>';
    			}
    		} else {
    			html += options.join('');
    		}
	    	$jq.append(html);
		}
	    
	    if (editable) {
	    	$jq.chosen($.extend(true, {}, {
	    		create_option: true,
	    	    persistent_create_option: true,
	    	    skip_no_results: true,
	    	    create_option_text: '添加',
	    	    disable_search_threshold: 5,
	            maxHeight : 200
	        }, options));
        } else {
        	$jq.chosen($.extend(true, {}, {
        		no_results_text: '未匹配到选项',
        		disable_search_threshold: 5,
	            maxHeight : 200
	        }, options));
        }
	    
	    isMultiple || $jq.val(initValue);
	    $jq.trigger("chosen:updated");
	    
	    return $jq;
	}
}(jQuery);

$(function () {
    $(".datepicker").datepicker({
        language: "zh-CN",
        autoclose: true,//选中之后自动隐藏日期选择框
        clearBtn: true,//清除按钮
        todayBtn: true,//今日按钮
        format: "yyyy-mm-dd"//日期格式，详见 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format
    });
});
